package com.xdtech.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.init.InitCacheData;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.dao.CodeValueDao;
import com.xdtech.sys.dao.DictionaryCodeDao;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.model.DictionaryCode;
import com.xdtech.sys.service.DictionaryCodeService;
import com.xdtech.sys.vo.CodeValueItem;
import com.xdtech.sys.vo.DictionaryCodeItem;
import com.xdtech.web.model.Pagination;
@Service
public class DictionaryCodeServiceImpl implements DictionaryCodeService{
	@Autowired
	private DictionaryCodeDao dictionaryCodeDao;
	@Autowired
	private CodeValueDao codeValueDao;
	public void save(DictionaryCode entity) {
		dictionaryCodeDao.save(entity);
	}

	public void delete(DictionaryCode entity) {
		dictionaryCodeDao.delete(entity);
	}

	public void delete(Long id) {
		dictionaryCodeDao.delete(id);
	}

	public DictionaryCode get(Long id) {
		return dictionaryCodeDao.get(id);
	}

	public List<DictionaryCode> getAll() {
		return dictionaryCodeDao.getAll();
	}

	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page<DictionaryCode> page = dictionaryCodeDao.findPage(pg);
		List<?> dictionarys = BeanUtils.copyListProperties(DictionaryCodeItem.class, page.getResult());
		results.put("total", page.getTotalItems());
		results.put("rows", dictionarys);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-1-26下午9:48:01
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateGoods(DictionaryCodeItem item) {
		DictionaryCode dictionaryCode = null;
		if (item.getId()==null) {
			dictionaryCode = new DictionaryCode();
		}else {
			dictionaryCode = dictionaryCodeDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(dictionaryCode, item);
		dictionaryCodeDao.save(dictionaryCode);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-1-26下午9:52:48
	 * @modified by
	 * @param dictionaryId
	 * @return
	 */
	public DictionaryCodeItem loadDictionaryCodeItem(Long dictionaryId) {
		DictionaryCode dictionaryCode = dictionaryCodeDao.get(dictionaryId);
		DictionaryCodeItem dictionaryCodeItem = new DictionaryCodeItem();
		BeanUtils.copyProperties(dictionaryCodeItem, dictionaryCode);
		return dictionaryCodeItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-1-26下午10:27:14
	 * @modified by
	 * @param idList
	 */
	public void deleteDictionaries(List<Long> idList) {
		for (Long id : idList) {
			codeValueDao.batchExecute("delete from CodeValue where dictionaryCode.id=?", id);
			DictionaryCode code = dictionaryCodeDao.get(id);
			dictionaryCodeDao.delete(code);
			//更新缓存,移除数据字典缓存
			InitCacheData.dictionary.remove(code.getKey());
		}
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-1-27下午11:19:31
	 * @modified by
	 * @param dictionaryCode
	 */
	public void saveDictionaryCodeValues(DictionaryCode dictionaryCode) {
		dictionaryCodeDao.save(dictionaryCode);
		codeValueDao.deleteByCodeId(dictionaryCode.getId());
		List<CodeValue> codeValues = dictionaryCode.getCodeValues();
		List<CodeValueItem> items = new ArrayList<CodeValueItem>();
		CodeValueItem item = null;
		for (CodeValue codeValue : codeValues) {
			codeValue.setDictionaryCode(dictionaryCode);
			codeValue.setId(null);
			codeValueDao.save(codeValue);
			item = new CodeValueItem();
			BeanUtils.copyProperties(item, codeValue);
			items.add(item);
		}
		// 更新字典值缓存
		InitCacheData.dictionary.put(dictionaryCode.getKey(), items);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-2-15下午8:47:28
	 * @modified by
	 * @param entities
	 */
	@Override
	public void saveAll(List<DictionaryCode> entities) {
		dictionaryCodeDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:24:21
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
